﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;

namespace DataLib.DAL
{
    /// <summary>
    /// 数据访问类:BarCode
    /// </summary>
    public partial class PEmail
    {



        /// <summary>
        /// 返回查询的邮件列表List
        /// </summary>
        public List<Model.PEmail> GetEmalList(string key, string oType)
        {
            StringBuilder strSql = new StringBuilder();
            if (key == null)
                key = "";

            switch (oType)
            {
                case "T"://原料理货
                    strSql.Append("select id,tname as lable,email as value from TallyEmail where (tname + email) like '%" + key + "%' ");
                    break;
                case "I"://销售询价
                    strSql.Append("select id,linkman as lable,email as value  from DoCustomer where (linkman + email) like '%" + key + "%' ");
                    break;
                case "CC"://抄送
                    strSql.Append("select id,Name as lable,email as value from CCEmail where (Name + email) like '%" + key + "%' ");
                    break;
                default:
                    break;
            }


            List<Model.PEmail> list = new List<Model.PEmail>();
            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(DataRowToModel(r));
                }
            }
            return list;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.PEmail DataRowToModel(DataRow row)
        {
            DataLib.Model.PEmail model = new DataLib.Model.PEmail();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["lable"] != null)
                {
                    model.lable = row["lable"].ToString();
                }
                if (row["value"] != null)
                {
                    model.value = row["value"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获取JSON
        /// </summary>
        /// <param name="key"></param>
        /// <param name="oType"></param>
        /// <returns></returns>
        public string GetJsonPEmail(string key, string oType)
        {
            string result = "";
            List<Model.PEmail> list = GetEmalList(key, oType);
            if (list.Count > 0)
                result = ObjectToJson(list);
            else
               result = "[]";
            return result;
        }

        //List转成json
        public string ObjectToJson<T>(IList<T> IL)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

       /// <summary>
       /// 根据多个邮件返回邮件人列表
       /// </summary>
       /// <param name="elist"></param>
       /// <param name="oType"></param>
       /// <returns></returns>
        public List<Model.PEmail> GetEList(string elist, string oType)
        {
            StringBuilder strSql = new StringBuilder();
            string p1;
            p1 = elist.Replace(",", "','");
            p1 = "'" + p1 + "'";

            switch (oType)
            {
                case "T"://原料理货
                    strSql.Append("select id,tname as lable,email as value from TallyEmail where email in (" + p1 + ")");
                    break;
                case "I"://销售询价
                    strSql.Append("select id,companyname as lable,email as value  from DoCustomer where email in (" + p1 + ")");
                    break;
                case "CC"://抄送
                    strSql.Append("select id,Name as lable,email as value from CCEmail where  email in (" + p1 + ")");
                    break;
                default:
                    break;
            }


            List<Model.PEmail> list = new List<Model.PEmail>();
            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(DataRowToModel(r));
                }
            }
            return list;
        }
    }
}

